package com.penghaisoft.microwms.product.web.controller.insidewarehouse;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.ProStorageDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.wms.model.ProStorageSn;
import com.penghaisoft.microwms.common.wms.model.RawLockLog;
import com.penghaisoft.microwms.common.wms.model.RawStorageSn;
import com.penghaisoft.microwms.product.service.IProStorageSnService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description 成品库内管理相关Controller
 * @author luoteng
 * @date 2018年6月6日 下午3:02:17 
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/insidewarehouse")
@Slf4j
public class InsideWarehouseController extends BaseController {

	@Autowired
	private IProStorageSnService proStorageSnService;
	/**
	 * @Title storagesnList
	 * @Description 成品库存明细查询
	 * @param page
	 * @param rows
	 * @param proStorageSn
	 * @return
	 */
	@PostMapping("storagesn/list")
	public TransResult storagesnList(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, ProStorageSn proStorageSn) {
		proStorageSn.preQuery();
		Pager<ProStorageSn> result = proStorageSnService.findListByCondition(page, rows, proStorageSn);
		return success(result);
	}
	/**
	 * @Title storagesnList
	 * @Description 成品库存查询
	 * @param page
	 * @param rows
	 * @param proStorageDto
	 * @return
	 */
	@PostMapping("storage/list")
	public TransResult storageList(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, ProStorageDto proStorageDto) {

		Pager<ProStorageDto> result = proStorageSnService.findStorageListByCondition(page, rows, proStorageDto);
		return success(result);
	}

	/**
	 * @wzhg 18619
	 * @param id
	 * @return
	 */
	@GetMapping("findById/{id}")
	public TransResult findById(@PathVariable("id")String id){
		ProStorageSn proStorageSn = new ProStorageSn();
		if(id != null && !"".equals(id)){
			proStorageSn = proStorageSnService.findById(id);
		}
		return success(proStorageSn);
	}
	/** 
	* @Title: findByIdAbs 
	* @Description: 记账出库修改，数量取正值 
	* @param @param id
	* @param @return  参数说明 
	* @return TransResult    返回类型
	* @author jzh 
	* @throws 
	*/
	@GetMapping("findByIdAbs/{id}")
	public TransResult findByIdAbs(@PathVariable("id")String id){
		ProStorageSn proStorageSn = new ProStorageSn();
		if(id != null && !"".equals(id)){
			proStorageSn = proStorageSnService.findById(id);
			BigDecimal absAmount =  proStorageSn.getAmount().abs();
			proStorageSn.setAmount(absAmount);
		}
		return success(proStorageSn);
	}

	/**
	 * 根据id修改合同号(可以置为空可以修改为其他值)
	 * @wzhg 1865
	 * @param rawStorageSn
	 * @return
	 */
	@PostMapping("updateById")
	public TransResult updateById(RawStorageSn rawStorageSn , ProStorageSn proStorageSn){
		Date now = new Date();
		proStorageSn.setLastModifiedBy(getLoginName());
		proStorageSn.setGmtModified(now);
		proStorageSnService.update(proStorageSn);
		return success();
	}
	@GetMapping("storagesn/r/detail")
	public TransResult findRawStorageSnByCode(ProStorageSn proStorageSn) {
		proStorageSn.preQuery();
		List<ProStorageSn> list = proStorageSnService.findProStorageSnByCondition(proStorageSn);
		return success(list);
	}
	/**
	 * 成品冻结解冻
	 * @param list
	 * @return
	 */
	@PostMapping("storagesn/lockAndunlock")
	public TransResult updateList(@RequestBody List<ProStorageSn> list) {
		TransResult reuslt = new TransResult();
		String currentUserName = getLoginName();
		RawLockLog rawLockLog = new RawLockLog();
		rawLockLog.setLockBy(currentUserName);
		Resp resp = new Resp();
		try {
		 resp = proStorageSnService.updateProStorageSnStatus(list,rawLockLog);
		} catch(RuntimeException e){
			reuslt.setCode("0");
			System.out.print(e.getMessage());
			reuslt.setMsg(e.getMessage());
			return reuslt;
		}
		return fromResp(resp);
	}
	
	/** 
	* @Title: editProStorageSnIn 
	* @Description: 成品入库明细新增，修改
	* @param @param proStorageSn
	* @param @return  参数说明 
	* @return TransResult    返回类型
	* @author jzh 
	* @throws 
	*/
	@PostMapping("prosnin/edit")
	public TransResult editProStorageSnIn(ProStorageSn proStorageSn) {
		Date now = new Date();
		if(proStorageSn.getStorageSnId() == null || "".equals(proStorageSn.getStorageSnId())){
			proStorageSn.setActiveFlag("1");
			proStorageSn.setBarcodeStatus("0");
			proStorageSn.setCreateBy(getLoginName());
			proStorageSn.setGmtCreate(now);
			proStorageSnService.create(proStorageSn);
		}else{
			proStorageSn.setLastModifiedBy(getLoginName());
			proStorageSn.setGmtModified(now);
			proStorageSnService.update(proStorageSn);
		}
		return success();
	}
	
	/** 
	* @Title: deleteProStorageSn 
	* @Description: 成品入库明细删除
	* @param @param proStorageSn
	* @param @return  参数说明 
	* @return TransResult    返回类型
	* @author jzh 
	* @throws 
	*/
	@PostMapping("delectprosn")
	public TransResult deleteProStorageSn(ProStorageSn proStorageSn) {
		Date now = new Date();
//		有效标志 1-有效 0-无效
		proStorageSn.setActiveFlag("0");
		proStorageSn.setLastModifiedBy(getLoginName());
		proStorageSn.setGmtModified(now);
		proStorageSnService.update(proStorageSn);
		return success();
	}
	

	/** 
	* @Title: chargeListOut 
	* @Description: 记账出库明细查询
	* @param @param page
	* @param @param rows
	* @param @param proStorageSn
	* @param @return  参数说明 
	* @return TransResult    返回类型
	* @author jzh 
	* @throws 
	*/
	@PostMapping("charge/list/out")
	public TransResult chargeListOut(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, ProStorageSn proStorageSn) {
		proStorageSn.preQuery();
		Pager<ProStorageSn> result = proStorageSnService.findChargeOut(page, rows, proStorageSn);
		return success(result);
	}
	
	/** 
	* @Title: editProStorageSnOut 
	* @Description: 记账出库修改
	* @param @param proStorageSn
	* @param @return  参数说明 
	* @return TransResult    返回类型
	* @author jzh 
	* @throws 
	*/
	@PostMapping("prosnout/edit")
	public TransResult editProStorageSnOut(ProStorageSn proStorageSn) {
		TransResult result = new TransResult();
		Date now = new Date();
		if(proStorageSn.getStorageSnId() == null || "".equals(proStorageSn.getStorageSnId())){
			proStorageSn.setActiveFlag("1");
			proStorageSn.setBarcodeStatus("0");
			proStorageSn.setCreateBy(getLoginName());
			proStorageSn.setGmtCreate(now);
			if(proStorageSn.getAmount() != null){
				proStorageSn.setAmount(proStorageSn.getAmount().negate());
			}else{
				result.setCode(Constant.FAIL);
				result.setMsg("请输入出库数量！");
				return result;
			}
			Resp resp = proStorageSnService.createChargeOut(proStorageSn);
			result.setCode(resp.getCode());
			result.setMsg(resp.getMsg());
			return result;
		}else{
			proStorageSn.setLastModifiedBy(getLoginName());
			proStorageSn.setGmtModified(now);
			if(proStorageSn.getAmount() != null){
				proStorageSn.setAmount(proStorageSn.getAmount().negate());
			}
			Resp resp = proStorageSnService.updateChargeOut(proStorageSn);
			result.setCode(resp.getCode());
			result.setMsg(resp.getMsg());
			return result;
		}
	}
	
	/** 
	* @Title: contactProUpdate 
	* @Description: 批量修改成品合同号
	* @param @param proStorageSn
	* @param @return  参数说明 
	* @return TransResult    返回类型
	* @author jzh 
	* @throws 
	*/
	@PostMapping("procontact/update")
	public TransResult contactProUpdate(ProStorageSn proStorageSn){
		TransResult result = new TransResult();
		String LoginName = getLoginName();
		proStorageSn.setLastModifiedBy(LoginName);
		result.setCode(Constant.SUCCESS);
		if(proStorageSn.getContractNoOld().equals(proStorageSn.getContractNo())){
			result.setCode(Constant.FAIL);
			result.setMsg("修改合同号与原合同号相同，请更换！");
			return result;
		}
		try{
			Resp resp = proStorageSnService.batchUpdateProContact(proStorageSn);
			if(resp != null){
				result.setCode(resp.getCode());
				result.setMsg(resp.getMsg());
			}
		}catch(RuntimeException e){
			result.setCode(Constant.FAIL);
			result.setMsg(e.getMessage());
		}
		
		return result;
	}

}
